package jade;

import org.joml.Vector2f;
import org.lwjgl.BufferUtils;
import renderer.Shader;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import static org.lwjgl.opengl.GL20.*;
import static org.lwjgl.opengl.GL30.glBindVertexArray;
import static org.lwjgl.opengl.GL30.glGenVertexArrays;

public class LevelEditorScene extends Scene {

    private float[] vertexArray = {
            //position            //color
            100.5f, 0.5f, 0.0f,  1.0f, 0.0f, 0.0f, 1.0f,  //右下角 0
            0.5f, 100.5f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f,  //左上角 1
            100.5f, 100.5f, 0.0f,   0.0f, 0.0f, 1.0f, 1.0f,  //右上角 2
            0.5f, 0.5f, 0.0f,  1.0f, 1.0f, 0.0f, 1.0f, //左下角 3
    };

    //逆时针顺序
    private int[] elementArray = {
            2, 1, 0,
            0, 1, 3
    };

    private int vertexID, fragmentID, shaderProgram;

    private int vboID, eboID, vaoID;

    private Shader defaultShader;

    public LevelEditorScene() {

    }

    @Override
    public void init() {
        this.camera = new Camera(new Vector2f());
        defaultShader = new Shader("assets/shaders/default.glsl");

        defaultShader.compile();

        glEnable(GL_BLEND);
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

        //生成 VAO ，VBO 和 EBO 发送给GPU
        vaoID = glGenVertexArrays();
        glBindVertexArray(vaoID);

        //创建一个 FloatBuffer 用来存放顶点数据
        FloatBuffer vertexBuffer = BufferUtils.createFloatBuffer(vertexArray.length);
        vertexBuffer.put(vertexArray).flip();

        //创建VBO 上传到vertexBuffer
        vboID = glGenBuffers();
        glBindBuffer(GL_ARRAY_BUFFER, vboID);
        glBufferData(GL_ARRAY_BUFFER, vertexBuffer, GL_STATIC_DRAW);

        //索引缓冲区
        IntBuffer elementBuffer = BufferUtils.createIntBuffer(elementArray.length);
        elementBuffer.put(elementArray).flip();
        //元素缓冲区
        eboID = glGenBuffers();
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, eboID);
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, elementBuffer, GL_STATIC_DRAW);

        //创建顶点属性指针
        int positionSize = 3;
        int colorSize = 4;
        int floatSize = 4;

        int vertexSizeBytes = (positionSize + colorSize) * floatSize;

        glVertexAttribPointer(0, positionSize, GL_FLOAT, false, vertexSizeBytes, 0);

        glEnableVertexAttribArray(0);

        glVertexAttribPointer(1, colorSize, GL_FLOAT, false, vertexSizeBytes, positionSize * floatSize);

        glEnableVertexAttribArray(1);

    }

    @Override
    public void update(float dt) {
//        System.out.println(" FPS:" + 1.0f / dt);
        camera.position.x -= dt*50;
        defaultShader.use();
        defaultShader.uploadMat4f("uProjection", camera.getProjectionMatrix());
        defaultShader.uploadMat4f("uView", camera.getViewMatrix());

        glBindVertexArray(vaoID);

        glEnableVertexAttribArray(0);
        glEnableVertexAttribArray(1);

        glDrawElements(GL_TRIANGLES, elementArray.length, GL_UNSIGNED_INT, 0);

        glDisableVertexAttribArray(0);
        glDisableVertexAttribArray(1);

        glBindVertexArray(0);
        defaultShader.detach();
    }
}
